<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品分类" >
        <el-cascader style="width: 800px"  size="small"
                     v-model="categorizeArr" @change="changeProductCategorize"
                     :options="$store.state.factorDict.productCategorizeDict0"
                     :props="{value:'code',label:'chineseName' ,checkStrictly: true }"
                     clearable></el-cascader>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-select v-model="queryParams.productId" placeholder="请选择产品" clearable size="small">
          <el-option v-for="dict in productInfoOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品批次" prop="productBatch">
        <el-input  v-model="queryParams.productBatch" placeholder="请输入产品批次"
                   clearable  size="small"  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属企业" prop="enterpriseId">
        <el-cascader  v-model="enterpriseIdArr"  size="small"
                      :options="$store.state.factorDict.enterpriseDict" :show-all-levels="false"
                      :props="{value:'id',label:'enterpriseName',children:'children',checkStrictly:true,}"
                      clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbon:emissionPathLog:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="emissionPathLogList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50" />
<!--      <el-table-column label="门类" align="center" prop="level0" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--          {{selectDictLabel($store.state.factorDict.productCategorizeDict0, scope.row.level0,'code','chineseName')}}-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="大类" align="center" prop="level1" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--          {{selectDictLabel($store.state.factorDict.productCategorizeDict1, scope.row.level1,'code','chineseName')}}-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="中类" align="center" prop="level2" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--          {{selectDictLabel($store.state.factorDict.productCategorizeDict2, scope.row.level2,'code','chineseName')}}-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="小类" align="center" prop="level3" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--          {{selectDictLabel($store.state.factorDict.productCategorizeDict3, scope.row.level3,'code','chineseName')}}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
      <el-table-column label="所属企业" align="center" prop="enterpriseId" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{selectDictLabel($store.state.factorDict.enterpriseOptions, scope.row.enterpriseId,'id','enterpriseName')}}
        </template>
      </el-table-column>
      <el-table-column label="产品批次" align="center" prop="productBatch" :show-overflow-tooltip="true"/>
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="因子特征要素" align="center" prop="categorizeCode"  min-width="300" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--         <span v-if="scope.row.categorizeCode">-->
<!--           {{selectDictLabels($store.state.factorDict.categorizeInfoOptions, scope.row.categorizeCode,',','id','chineseName')}}-->
<!--         </span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <!--      <el-table-column label="上下游" align="center" prop="status" width="80" :show-overflow-tooltip="true">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{selectDictLabelDefault($store.state.factorDict.factorTypeOptions, scope.row.factorType)}}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="新排放因子值" align="center" prop="factorNum" :show-overflow-tooltip="true"/>
      <el-table-column label="旧排放因子值" align="center" prop="oldFactorNum" :show-overflow-tooltip="true"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listEmissionPathLog, getEmissionPathLog, delEmissionPathLog, addEmissionPathLog, updateEmissionPathLog, exportEmissionPathLog } from "@/api/carbon/emissionPathLog";
import { listProductInfo } from "@/api/factor/productInfo";
export default {
  name: "EmissionPathLog",
  data() {
    return {
      //产品库
      productInfoOptions:[],
      enterpriseIdArr:[],
      categorizeArr:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 减排路径记录表格数据
      emissionPathLogList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        level0: null,
        level1: null,
        level2: null,
        level3: null,
        productName: null,
        productId: null,
        enterpriseId: null,
        productBatch: null,
        categorizeCode: null,
        factorType: null,
        factorNum: null,
        oldFactorNum: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeProductCategorize(){
      let params={};
      if(this.categorizeArr.length>0){
        params.level0 = this.categorizeArr[0]
      }
      if(this.categorizeArr.length>1){
        params.level1 = this.categorizeArr[1]
      }
      if(this.categorizeArr.length>2){
        params.level2 = this.categorizeArr[2]
      }
      if(this.categorizeArr.length>3){
        params.level3 = this.categorizeArr[3]
      }
      listProductInfo(params).then(response => {
        this.productInfoOptions = response.rows;
      });
    },

    /** 查询减排路径记录列表 */
    getList() {
      if(this.enterpriseIdArr.length>0){
        this.queryParams.enterpriseId = this.enterpriseIdArr[this.enterpriseIdArr.length-1];
      }else {
        this.queryParams.enterpriseId = null;
      }
      if(this.categorizeArr.length>0){
        this.queryParams.level0 = this.categorizeArr[0]
      }else {
        this.queryParams.level0 = null;
      }
      if(this.categorizeArr.length>1){
        this.queryParams.level1 = this.categorizeArr[1]
      }else {
        this.queryParams.level1 = null;
      }
      if(this.categorizeArr.length>2){
        this.queryParams.level2 = this.categorizeArr[2]
      }else {
        this.queryParams.level2 = null;
      }
      if(this.categorizeArr.length>3){
        this.queryParams.level3 = this.categorizeArr[3]
      }else {
        this.queryParams.level3 = null;
      }
      this.loading = true;
      listEmissionPathLog(this.queryParams).then(response => {
        this.emissionPathLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.enterpriseIdArr=[];
      this.categorizeArr=[];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmissionPathLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
